<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use think\Request;

/**
 * 首页接口
 */
class Huanxin extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页 注册所有人
     */
    public function index()
    {   
        $m_userhome = Db::name("userhome");

        require_once "./huanxin/Easemob.class.php";
        $e = new \Easemob();

        $user_list = $m_userhome->field("id,nickname,avatar")->select();
        foreach ($user_list as $i => $v) {
            #注册环信
            $e->createUser($user_list[$i]['id'],"123456",$user_list[$i]['nickname']);
            #设置属性  头像、昵称
            $e->editUser($user_list[$i]['id'],$user_list[$i]['nickname'],path($user_list[$i]['avatar']));
        }

        $this->success('请求成功');
    }


    /*
    * 环信的所有群
    */
    public function huanxin_group_list(){
        require_once "./huanxin/Easemob.class.php";
        $e = new \Easemob();
        //传入用户名和登录密码参数注册为IM用户
        $result_u = $e->getGroups();
        dump($result_u);
    }

    /*
    * 删除群
    */
    public function huanxin_group_delete(){
        require_once "./huanxin/Easemob.class.php";
        $e = new \Easemob();
        //传入用户名和登录密码参数注册为IM用户
        $result_u = $e->deleteGroup("161778812125185");
        dump($result_u);
    }

    /*
    * 删除所有用户
    */
    public function user_all_delete(){
        require_once "./huanxin/Easemob.class.php";
        $e = new \Easemob();
        //传入用户名和登录密码参数注册为IM用户
        $result_u = $e->getUsers(100);
        $list = $result_u['entities'];
        foreach ($list as $i => $v) {
            $e->deleteUser($list[$i]['username']);
        }
    }

    /*
    * 用户列表
    */
    public function user_list(){
        require_once "./huanxin/Easemob.class.php";
        $e = new \Easemob();
        //传入用户名和登录密码参数注册为IM用户
        $result_u = $e->getUsers(100);
        dump($result_u);
    }

    /*
    * 用户删除
    */
    public function user_delete(){
        require_once "./huanxin/Easemob.class.php";
        $e = new \Easemob();
        //传入用户名和登录密码参数注册为IM用户
        $result_u = $e->deleteUser("3");
        dump($result_u);
    }


    /**
     * 在环信即时通讯系统注册为IM用户
     * @param $username 用户名
     * @param $password 登录密码
     * @param $nickname 昵称
     */
    public function user_add()
    {
        require_once "./huanxin/Easemob.class.php";
        $e = new \Easemob();
        //传入用户名和登录密码参数注册为IM用户
        $result_u = $e->createUser('3', '123456','飞啊飞');
        dump($result_u);
    }

    /**
     * 设置用户属性
     * @param $username 用户名
     */
    public function user_edit()
    {
        require_once "./huanxin/Easemob.class.php";
        $e = new \Easemob();
        //传入用户名和登录密码参数注册为IM用户
        $result_u = $e->editUser('3','飞啊飞','123123');
        dump($result_u);
    }

    /**
     * 查询用户属性
     * @param $username 用户名
     */
    public function user_index()
    {
        require_once "./huanxin/Easemob.class.php";
        $e = new \Easemob();
        //传入用户名和登录密码参数注册为IM用户
        $result_u = $e->indexUser('7');
        dump($result_u);
    }

    /**
     * 获取群成员
     * @param $group_id 用户名
     */
    public function group_user_index()
    {
        require_once "./huanxin/Easemob.class.php";
        $e = new \Easemob();
        //传入用户名和登录密码参数注册为IM用户
        $result_u = $e->getGroupUsers('162057073786881');
        
        $user_data1 = array_column($result_u['data'],'owner');
        $user_data2 = array_column($result_u['data'],'member');
        
        $user_data1 = $e->getGroupUsersdetail($user_data1);
        $user_data2 = $e->getGroupUsersdetail($user_data2);
        dump($user_data1);
        dump($user_data2);
    }




}
